<?php

declare(strict_types=1);

namespace app\model;

use think\facade\Db;
use think\Model;

/**
 * 
 *
 * @mixin \think\Model
 * @property int $id
 * @property bool $serv_type 和代理商业务取值一直见 constant.php->PRODUCT_SERV_TYPE
 * @property bool $sub_type 业务下的子商品业务。1慢充2快充 。比如加油没有字类就不需要用该字段
 * @property string $name
 * @property string $deleted_at
 * @property string $type_name 产品类型
 */
class ServiceSub extends CommonModel
{


    const SERV_TYPE_SLOW = 1;
    const SERV_TYPE_FAST = 2;
    const SERV_TYPE_INTERFACE = 3;
    const SERV_TYPE_INTERFACE_AND_COMMISSION = 4;

    /*
     批量添加
     trancate 是否先清空
     该数据由于
    */
    public function batchAdd($trancate = 1)
    {
        // 暂不开启事务
        if ($trancate) {
            Db::query('truncate table '.$this->getTable());
        }
        $servTypeArr = PRODUCT_SERV_TYPE;
        $data = [];
        foreach ($servTypeArr as $k => $v) {
            $temp = [
                'serv_type' => $k,
                'name' => $v['name'],
            ];
            if (in_array($k, [PRODUCT_MOBILE_SERV, PRODUCT_OILCARD_SERV])) {
                // 快慢充充值类型
                $temp['sub_type'] = 1;
                $temp['name'] = $v['name'].'慢充';
                $data[] = $temp;

                $temp['name'] = $v['name'].'快充';
                $temp['sub_type'] = 2;
            } elseif (in_array($k, [PRODUCT_MEAL_SERV, PRODUCT_METUAN_SERV])) {
                // 外卖 美团到店 那些支付在供应商的
                $temp['sub_type'] = 3;
            } else {
                $temp['sub_type'] = 0;
            }
            $data[] = $temp;
        }

        return $this->saveAll($data);
    }
}
